<?php

namespace core\lib;

class Model
{

    public static $medoo;

    public function __construct()
    {
        if (is_null(self::$medoo)) {
            $database = Config::all("database");
            self::$medoo = new Medoo($database);

        }
        return self::$medoo;
    }

    public function action()
    {
        self::$medoo->pdo->beginTransaction();
    }

    public function run()
    {
        self::$medoo->pdo->commit();
    }

    public function get($table, $join = null, $columns = null, $where = null)
    {
        return self::$medoo->get($table, $join, $columns, $where);
    }

    public function select($table, $join, $columns = null, $where = null)
    {
        return self::$medoo->select($table, $join, $columns, $where);
    }

    public function update($table, $data, $where = null)
    {
        return self::$medoo->update($table, $data, $where);
    }

    public function insert($table, $datas)
    {
        return self::$medoo->insert($table, $datas);
    }

    public function delete($table, $where)
    {
        return self::$medoo->delete($table, $where);
    }

    public function count($table, $join = null, $column = null, $where = null)
    {
        return self::$medoo->count($table, $join, $column, $where);
    }

    public function avg($table, $join, $column = null, $where = null)
    {
        return self::$medoo->avg($table, $join, $column, $where);
    }

    public function max($table, $join = null, $column = null, $where = null)
    {
        return self::$medoo->max($table, $join, $column, $where);
    }

    public function min($table, $join, $column = null, $where = null)
    {
        return self::$medoo->min($table, $join, $column, $where);
    }

    public function sum($table, $join, $column = null, $where = null)
    {
        return self::$medoo->sum($table, $join, $column, $where);
    }

    public function has($table, $join, $where = null)
    {
        return self::$medoo->has($table, $join, $where);
    }

    public function id()
    {
        return self::$medoo->id();
    }

    public function debug()
    {
        return self::$medoo->debug();
    }

    public function doo()
    {
        $error = self::$medoo->error();
        if ($error['0'] == '0000') {
            return true;
        } else {
            return false;
        }
    }

    public function log()
    {
        return self::$medoo->log();
    }

    public function rand($table, $column = '*', $where = [])
    {
        return self::$medoo->rand($table, $column, $where);
    }

    public function raw($string, $map = [])
    {
        return self::$medoo->raw($string, $map);
    }

    public function query($query, $map = [])
    {
        return self::$medoo->query($query, $map = []);
    }
}